package com.community.backend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.community.backend.mapper.UserMapper;
import com.community.backend.pojo.User;
import com.community.backend.pojo.dto.LoginDTO;
import com.community.backend.pojo.dto.RegisterDTO;
import com.community.backend.pojo.dto.UserQueryDTO;
import com.community.backend.pojo.vo.UserQueryVO;
import com.community.backend.pojo.vo.UserRoomVO;
import com.community.backend.pojo.dto.UserDTO;
import com.community.backend.service.UserService;
import com.community.backend.utils.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public void register(RegisterDTO registerDTO) {
        User user = new User();
        BeanUtil.copyProperties(registerDTO, user);
        user.setId(UUID.randomUUID().toString());
        user.setStatus("已注册");
        user.setIsDelete("未删除");
        System.out.println("注册的用户信息"+ user);
        userMapper.addUser(user);
    }

    @Override
    public User selectById(String id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public User login(LoginDTO loginDTO) {
        User info = userMapper.login(loginDTO.getUsername(), loginDTO.getPassword());
        return info;
    }


    @Override
    public PageResult userByPage(UserQueryDTO userQueryDTO) {
        PageHelper.startPage(userQueryDTO.getCurrentPage(), userQueryDTO.getPageSize());
        Page<UserQueryVO> page = userMapper.pageQuery(userQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void update(User user) {
        Integer rows = userMapper.update(user);

    }

    @Override
    public void deleteById(String id) {
        User user = new User();
        user.setId(id);
        Integer rows = userMapper.updateById(user);

    }

    @Override
    public void addUser(User user) {
        user.setId(UUID.randomUUID().toString());
        user.setUsername(user.getName());
        user.setPassword("123456");
        user.setStatus("禁用");
        user.setIsDelete("未删除");
        System.out.println("注册的用户信息"+ user);
        Integer rows = userMapper.addUser(user);

    }

    @Override
    public List<UserRoomVO> searchRoom(UserDTO userDTO) {
        List<UserRoomVO> list = userMapper.searchRoom(userDTO);
        return list;
    }


}
